package org.ims.inv.service.worklog.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.ims.inv.dao.worklog.DAOWorkDailyLog;
import org.ims.inv.model.worklog.ModelWorkBaseLog;
import org.ims.inv.model.worklog.ModelWorkDailyLog;
import org.ims.inv.service.worklog.ServiceWorkDailyLog;
import org.ims.inv.web.util.ContextUtil;

import cn.trymore.core.exception.ServiceException;
import cn.trymore.core.service.impl.ServiceGenericImpl;
import cn.trymore.core.util.UtilDate;
import cn.trymore.core.util.UtilString;
import cn.trymore.core.web.paging.PaginationSupport;
import cn.trymore.core.web.paging.PagingBean;

/**
 * Service: 工作日志 - Daily
 * 
 * @author Jeccy.Zhao
 *
 */
public class ServiceWorkDailyLogImpl
extends ServiceGenericImpl<ModelWorkDailyLog> implements ServiceWorkDailyLog
{
	/**
	 * The work daily log repository.
	 */
	private DAOWorkDailyLog daoWorkDailyLog;
	
	/**
	 * The sole constructor.
	 * 
	 * @param dao
	 */
	public ServiceWorkDailyLogImpl(DAOWorkDailyLog dao)
	{
		super(dao);
		this.daoWorkDailyLog = dao;
	}
	
	public DAOWorkDailyLog getDaoWorkDailyLog()
	{
		return daoWorkDailyLog;
	}

	public void setDaoWorkDailyLog(DAOWorkDailyLog daoWorkDailyLog)
	{
		this.daoWorkDailyLog = daoWorkDailyLog;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ims.inv.service.worklog.ServiceWorkDailyLog#getWeekDailyLogByDate(java.lang.String)
	 */
	@Override
	public List<ModelWorkDailyLog> getWeekDailyLogByDate(String date, Integer category)
			throws ServiceException
	{
		try
		{
			int year = UtilDate.getCurrentYear(UtilDate.toDate(date));
			int weekNum = Integer.valueOf(UtilDate.getWeekNumber(UtilDate.toDate(date)));
			int maxWeekNum = UtilDate.getMaxWeekNumOfYear(year);
			
			if (weekNum > maxWeekNum)
			{
				weekNum = 1;
				year = year + 1;
			}
			
			if (weekNum == 1 && date.contains("-12-"))
			{
				year = year + 1;
			}
			
			Date startDate = UtilDate.getFirstDayOfWeek(year, weekNum);
			Date endDate = UtilDate.getLastDayOfWeek(year, weekNum);
			
			ModelWorkDailyLog criterion = new ModelWorkDailyLog();
			
			if (ModelWorkBaseLog.EWrokLogCategory.PERSONAL.getValue().equals(category))
			{
				criterion.setLogOwner(ContextUtil.getCurrentUser());
			}
			
			criterion.setLogCategory(category);
			
			DetachedCriteria criteria = getCriterias(criterion);
			criteria.addOrder(Order.asc("logDate"));
			criteria.add(Restrictions.sqlRestriction(
				"log_date >= '" + UtilDate.parseTime(startDate, "yyyy-MM-dd") + "' and log_date <= '" + UtilDate.parseTime(endDate, "yyyy-MM-dd") + "'"));
			
			return this.getByCriterion(criteria);
		}
		catch (Exception e)
		{
			throw new ServiceException(e);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ims.inv.service.worklog.ServiceWorkDailyLog#getWeekDailyLogByUsers(java.lang.String, java.lang.String, java.lang.String[])
	 */
	@Override
	public List<ModelWorkDailyLog> getWeekDailyLogByUsers (String startDate, 
			String endDate, String[] userIds) throws ServiceException
	{
		DetachedCriteria criteria = this.getCriterias(null);
		
		if (UtilString.isNotEmpty(startDate))
		{
			criteria.add(Restrictions.sqlRestriction("log_date >= '" + startDate + "'"));
		}
		
		if (UtilString.isNotEmpty(endDate))
		{
			criteria.add(Restrictions.sqlRestriction("log_date <= '" + endDate + "'"));
		}
		
		if (userIds != null && userIds.length > 0)
		{
			criteria.add(Restrictions.sqlRestriction("log_uid in (" + UtilString.join(userIds, ",") + ")"));
		}
		
		return this.getByCriterion(criteria);
	}

	/*
	 * (non-Javadoc)
	 * @see org.ims.inv.service.worklog.ServiceWorkDailyLog#getPaginationDailyLogByRangeDate(java.lang.String, java.lang.String, org.ims.inv.model.worklog.ModelWorkDailyLog, cn.trymore.core.web.paging.PagingBean)
	 */
	@Override
	public PaginationSupport<ModelWorkDailyLog> getPaginationDailyLogByRangeDate(String startDate,
			String endDate, ModelWorkDailyLog domain, PagingBean pagingBean) throws ServiceException
	{
		DetachedCriteria criteria = this.getCriterias(domain);
		
		if (UtilString.isNotEmpty(startDate))
		{
			criteria.add(Restrictions.sqlRestriction("log_date >= '" + startDate + "'"));
		}
		
		if (UtilString.isNotEmpty(endDate))
		{
			criteria.add(Restrictions.sqlRestriction("log_date <= '" + endDate + "'"));
		}
		
		return this.getPagination(criteria, pagingBean);
	}
	
	/*
	 * (non-Javadoc)
	 * @see cn.trymore.core.service.impl.ServiceGenericImpl#getCriterias(cn.trymore.core.model.ModelBase)
	 */
	@Override
	protected DetachedCriteria getCriterias(ModelWorkDailyLog domain)
	{
		DetachedCriteria criteria = DetachedCriteria.forClass(ModelWorkDailyLog.class);
		
		if (domain != null)
		{
			if (domain.getLogDate() != null)
			{
				criteria.add(Restrictions.sqlRestriction(
					"log_date = '" + (UtilDate.parseTime(domain.getLogDate(), "yyyy-MM-dd")) + "'"
				)); 
			}
			
			if (domain.getLogCategory() != null)
			{
				criteria.add(Restrictions.eq("logCategory", domain.getLogCategory()));
			}
			
			if (domain.getLogOwner() != null)
			{
				criteria.createCriteria("logOwner").add(Restrictions.eq("id", domain.getLogOwner().getId()));
			}
			
			if (domain.getLogProject() != null)
			{
				criteria.createCriteria("logProject").add(Restrictions.eq("id", domain.getLogProject().getId()));
			}
			
			if (domain.getLogWorkType() != null)
			{
				criteria.createCriteria("logWorkType").add(Restrictions.eq("id", domain.getLogWorkType().getId()));
			}
		}
		
		criteria.addOrder(Order.asc("logDate"));
		
		return criteria;
	}

}
